<?php
namespace app\job;

use app\common\model\Member;
use EasyWeChat\Factory;
use think\Exception;
use think\Log;
use think\queue\Job;

class Message extends BaseJob
{

    /**
     * 公众号消息模板发送
     * @param Job $job
     * @param $data
     */
    public function handle(Job $job, $data)
    {
        try{
            $member_info = Member::get($data['member_id']);
            if ($member_info && !empty($member_info->wx_openid) && $member_info->subscribe == 1){
                $wxConfig = config('wechat_gzh');
                $app = Factory::officialAccount($wxConfig);
                $arr['touser'] = $member_info->wx_openid;
                $arr['template_id'] = $data['template_id'];
                $arr['url'] = isset($data['url']) ? $data['url'] : "";
                if (isset($data['pagepath']) && !empty($data['pagepath'])){
                    $arr['miniprogram'] = ['appid'=>config('wechat')['app_id'],'pagepath'=>$data['pagepath']];
                }
                $arr['data'] = $data['data'];
                Log::info($arr);
                $result = $app->template_message->send($arr);
                Log::info($result);
                if ($result['errcode'] != 0)
                    throw new Exception('发送失败');
            }
        }catch (Exception $e){
            Log::error($e->getMessage());
            throw $e;
        }
    }









}